Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

128
Vistas
Pass "this" for an element inside an array

so i am playing around with firefox addons. but this isnt important for this case cause its just simple dom javascript. so basically i want to give every image on an website an eventlistener that listens for clicks and if it gets one it should return the src of the clicked image. when i run my current code i'll get "undefined" as an result. I also already tried passing "images[i]" to the clickImg function at the listener but then i get all src values from all images on the current page, but that not what i want. I only want to get the src of the clicked image.

thanks for looking into this :)

const initSelMode = (request, sender, sendResponse) => {
    var images = document.querySelectorAll("img")
    for(var i = 0; i < images.length; i++){
        images[i].dataset.clickp = i
        images[i].addEventListener("click", clickImg)
  }
}

const clickImg = () => {
    var body = document.querySelector("body")
    var obj = document.createElement("p")
    obj.innerText = this.src
    body.appendChild(obj)
}

browser.runtime.onMessage.addListener(initSelMode)
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda